package com.ourteam.system.service;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.ourteam.system.dao.SysEventLog;
import com.ourteam.system.dao.SysEventLogDAOQueryBean;
import com.ourteam.system.dao.SysEventLogType;
import com.ourteam.system.dao.SysEventLogTypeDAOQueryBean;
import com.ourteam.system.domain.SystemEventLevelEnum;
import com.ourteam.system.domain.SystemEventLogBean;
import com.ourteam.system.domain.SystemEventLogTypeBean;

public class SystemEventLogServiceImpl extends AbstractSystemEventLogService {

	/**
	 * 
	 * 
	 * @param queryBean
	 * 
	 * @model
	 * @throws BusinessErrorException
	 * @throws Exception
	 */
	@Override
	protected com.ourteam.system.domain.SystemEventLogBean[] doQuerySystemEventLog(
			final com.ourteam.system.dao.SysEventLogDAOQueryBean queryBean)
			throws Exception {

		return SystemEventLogBean.toArray(this.sysEventLogDao.query(queryBean));

	}

	/**
	 * 
	 * 
	 * @model
	 * @throws BusinessErrorException
	 * @throws Exception
	 */
	@Override
	protected int doQuerySystemEventLogCount(SysEventLogDAOQueryBean queryBean)
			throws Exception {

		return this.sysEventLogDao.queryCount(queryBean);
	}

	/**
	 * 
	 * 
	 * @param systemEventLog
	 * 
	 * @model
	 * @throws BusinessErrorException
	 * @throws Exception
	 */
	@Override
	protected void doAddSystemEventLog(final String eventTypeCode,
			final com.ourteam.system.domain.SystemEventLogBean systemEventLog,
			final Object eventContentObject) throws Exception {

		SysEventLogTypeDAOQueryBean queryBean = new SysEventLogTypeDAOQueryBean();

		queryBean.setDistinct(true);

		queryBean.createCriteria().andEventTypeCodeEqualTo(eventTypeCode);

		SysEventLogType eventLogType = this.sysEventLogTypeDao.query(queryBean)[0];

		Map<String, Object> templateContext = new HashMap<String, Object>();

		templateContext.put("contentObject", eventContentObject);

		StringWriter contentWriter = new StringWriter();
		
		new VelocityEngine().evaluate(new VelocityContext(templateContext),
				contentWriter, "SysEventLog",
				new StringReader(eventLogType.getEventContentTemplate()));

		SysEventLog eventLog = systemEventLog.convertToSysEventLog();

		eventLog.setEventContent(contentWriter.toString());

		eventLog.setEventTypeId(eventLogType.getId());

		if (StringUtils.isEmpty(eventLog.getEventLevel())) {
			eventLog.setEventLevel(SystemEventLevelEnum.INFO.getName());
		}

		if (eventLog.getEventTime() == null) {
			eventLog.setEventTime(new Date());
		}

		this.sysEventLogDao.insert(eventLog);
	}

	
	
	
	
	
	/**
	 * 
	 * 
	 * @param systemEventLogId
	 * 
	 * @model
	 * @throws BusinessErrorException
	 * @throws Exception
	 */
	@Override
	protected com.ourteam.system.domain.SystemEventLogBean doGetSystemEventLogById(
			final long systemEventLogId) throws Exception {

		SysEventLog eventLog = this.sysEventLogDao.findById(systemEventLogId);

		return eventLog != null ? new SystemEventLogBean(eventLog) : null;
	}

	@Override
	protected SystemEventLogTypeBean[] doQuerySystemEventLogType(
			SysEventLogTypeDAOQueryBean queryBean) throws Exception {

		return SystemEventLogTypeBean.toArray(this.sysEventLogTypeDao
				.query(queryBean));
	}

	@Override
	protected int doQuerySystemEventLogTypeCount(
			SysEventLogTypeDAOQueryBean queryBean) throws Exception {

		return this.sysEventLogTypeDao.queryCount(queryBean);
	}

	@Override
	protected void doAddSystemEventLogType(
			SystemEventLogTypeBean systemEventLogType) throws Exception {

		if (this.sysEventLogTypeDao.checkIsUnique(systemEventLogType
				.convertToSysEventLogType()) == false) {
			throw new Exception("事件类型 '"
					+ systemEventLogType.getEventTypeCode() + "' 已存在");
		}

		this.sysEventLogTypeDao.insert(systemEventLogType
				.convertToSysEventLogType());

	}

	@Override
	protected void doModifySystemEventLogType(
			SystemEventLogTypeBean systemEventLogType) throws Exception {

		if (this.sysEventLogTypeDao.checkIsUnique(systemEventLogType
				.convertToSysEventLogType()) == false) {
			throw new Exception("事件类型 '"
					+ systemEventLogType.getEventTypeCode() + "' 已存在");
		}

		this.sysEventLogTypeDao.update(systemEventLogType
				.convertToSysEventLogType());

	}

	@Override
	protected void doDeleteSystemEventLogTypes(long[] systemEventLogTypeIds)
			throws Exception {

		if (ArrayUtils.isEmpty(systemEventLogTypeIds)) {
			return;
		}

		SysEventLogTypeDAOQueryBean queryBean = new SysEventLogTypeDAOQueryBean();

		queryBean.createCriteria().andIdIn(systemEventLogTypeIds);

		this.sysEventLogTypeDao.delete(queryBean);

	}

	@Override
	protected SystemEventLogTypeBean doGetSystemEventLogTypeById(
			long systemEventLogTypeId) throws Exception {

		SysEventLogType eventLogType = this.sysEventLogTypeDao
				.findById(systemEventLogTypeId);

		return eventLogType != null ? new SystemEventLogTypeBean(eventLogType)
				: null;
	}

}